﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormTest
{
    partial class UcListView
    {
        private int sortColumn = -1; // 当前排序列索引
        private SortOrder sortOrder = SortOrder.None; // 当前排序方向
        private class ListViewItemComparer : IComparer
        {
            private int column;
            private SortOrder order;

            public ListViewItemComparer(int column, SortOrder order)
            {
                this.column = column;
                this.order = order;
            }

            public int Compare(object x, object y)
            {
                ListViewItem itemX = x as ListViewItem;
                ListViewItem itemY = y as ListViewItem;

                if (itemX == null || itemY == null) return 0;

                string textX = itemX.SubItems[column].Text;
                string textY = itemY.SubItems[column].Text;

                // 尝试按数字比较
                if (int.TryParse(textX, out int numX) && int.TryParse(textY, out int numY))
                {
                    return CompareNumbers(numX, numY);
                }

                // 尝试按日期比较
                if (DateTime.TryParse(textX, out DateTime dateX) && DateTime.TryParse(textY, out DateTime dateY))
                {
                    return CompareDates(dateX, dateY);
                }

                // 默认为字符串比较
                return CompareStrings(textX, textY);
            }
            private int CompareNumbers(int x, int y)
            {
                return order == SortOrder.Ascending ? x.CompareTo(y) : y.CompareTo(x);
            }

            private int CompareDates(DateTime x, DateTime y)
            {
                return order == SortOrder.Ascending ? x.CompareTo(y) : y.CompareTo(x);
            }

            private int CompareStrings(string x, string y)
            {
                return order == SortOrder.Ascending ? string.Compare(x, y) : string.Compare(y, x);
            }
        }

        private void OnColumnClick(object sender, ColumnClickEventArgs e)
        {
            // 如果点击的是当前排序列，则切换排序方向
            if (e.Column == sortColumn)
            {
                sortOrder = sortOrder == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
            }
            else
            {
                // 新列排序，默认升序
                sortColumn = e.Column;
                sortOrder = SortOrder.Ascending;
            }
            //// 创建自定义比较器
            ListViewItemComparer comparer = new ListViewItemComparer(sortColumn, sortOrder);

            //// 进行排序
            ListViewItemSorter = comparer;
            Sort();
            if (e.Column > 3) return;
            // 重置所有列头文本
            for (int i = 0; i < this.Columns.Count; i++)
            {
                string originalText = this.Columns[i].Text.Trim(' ', '↑', '↓');
                this.Columns[i].Text = originalText;
            }
            // 为当前排序列添加指示符号
            if (sortColumn != -1 && sortOrder != SortOrder.None)
            {
                string sortIndicator = sortOrder == SortOrder.Ascending ? " ↑" : " ↓";
                this.Columns[sortColumn].Text += sortIndicator;
            }
        }
    }
}
